<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>
<%@page import="javazoom.upload.*"%> 
<%@page import="uploadutilities.FileMover"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="jxl.Cell"%>
<%@page import="jxl.Sheet"%>
<%@page import="jxl.Workbook"%>
<%@page import="jxl.WorkbookSettings"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@page import="db.Place"%>
<%@page import="db.PlaceDAO"%>

<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%@ include file="/jsp/common/utility/ImportExecl.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	PlaceDAO placeDAO = (PlaceDAO)ctx.getBean("PlaceDAO");
	FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
	UploadBean upBean = new UploadBean();
	int tid = (Integer)session.getAttribute("tid");
	MultipartFormDataRequest mrequest = null;
	Hashtable files = null;
	boolean success = true;
	String message = "";
	String serverPath = new String();
	
	if (MultipartFormDataRequest.isMultipartFormData(request))
	{
		mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"utf-8");//注意这里也要设置编码参数
		//这里用来测试title参数是否正确。调试的时候，加一句if (true)return;即可。
		files = mrequest.getFiles();
	}
	
	
	//获取修改前的文件名
	String sOldFileName =mrequest.getParameter("oldfilename"); 
	//out.println("sOldFileName:"+sOldFileName);
	String sWebRootPath = request.getRealPath("/");//得到你的web应用的根。
	String sPath=sWebRootPath+"allprinfoTemp";
	int iFileCount = 0;
	String sServerFileName="";
	String sLocalFileName = "";
	//文件获取
	if ( (files != null) || (!files.isEmpty()) ) {
		iFileCount = files.size();
		UploadFile file = (UploadFile) files.get("attach");
		sLocalFileName=file.getFileName();
		out.println("sLocalFileName:"+sLocalFileName);
		int ii= sLocalFileName.indexOf("."); //取文件名的后缀
		String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
		//得到不重复的文件名
		java.util.Date dt = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sServerFileName= fmt.format(dt);
		sServerFileName =sServerFileName + sExt;
		//如果不存在该目录，则新建一个
		File dir =new File(sPath);
		if (!dir.exists()){
			dir.mkdirs();
		}
		upBean.setFolderstore(sPath);//设置要上传的目录
		upBean.addUploadListener(fileMover);//增加filMover监听
		fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
		upBean.store(mrequest, "attach");//上传
		serverPath = sPath+"/"+sServerFileName;
		//System.out.println(serverPath);
	}else{
		success = false;
		message = "文件上传失败。";
	}
	List listjxl = new ArrayList();
	if(serverPath!=null){
		listjxl = getListByJxl(serverPath,success,message);
		request.setAttribute("serverPath",serverPath);
	}
	List listmap = new ArrayList();
	if(listjxl!=null && listjxl.size()>0){
		for(int i=0;i<listjxl.size();i++){
			List li = (List)listjxl.get(i);
			//System.out.println("execl文件格式行数不够===========>>>>>"+li.size());
			
			Map map = new HashMap();
			if(li!=null && li.size()>=3){
				List matlist = projectDAO.findByCode(li.get(0));
				if(matlist!=null && matlist.size()>0){
					for(int m=0;m<matlist.size();m++){
						Project project = (Project)matlist.get(m);
						if(project.getIsDelete()==0){
							map.put("projectcodebo",true);
							map.put("procode",li.get(0));
							map.put("proname",li.get(1));
							map.put("procom",li.get(2));
							success = false;
							message = "项目存在。";
						}
					}
				}else{
					map.put("projectcodebo",false);
					map.put("procode",li.get(0));
							map.put("proname",li.get(1));
							map.put("procom",li.get(2));
				}
			}else{
				success = false;
				message = "解析出错，execl文件格式行数不够，请仔细检查execl文件。";
			}
			listmap.add(map);
		}
	}
	request.setAttribute("listjxl",listmap);
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/prinfo/allPrinfo.vm");
%>